<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人信息设置</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: "Microsoft YaHei", sans-serif;
            background-color: #f5f6fa;
            padding: 20px;
            margin: 0;
        }

        .form-container {
            max-width: 600px;
            margin: 0 auto;
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        #formTitle {
            color: #2c3e50;
            font-size: 24px;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 2px solid #eee;
            text-align: center;
        }

        .form-group {
            margin-bottom: 25px;
        }

        label {
            display: block;
            color: #34495e;
            margin-bottom: 8px;
            font-weight: 500;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            transition: all 0.3s ease;
        }

        input[type="text"]:focus,
        input[type="password"]:focus {
            border-color: #3498db;
            box-shadow: 0 0 5px rgba(52,152,219,0.3);
            outline: none;
        }

        #submitBtn {
            background-color: #3498db;
            color: white;
            padding: 12px 25px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
            transition: background-color 0.3s ease;
        }

        #submitBtn:hover {
            background-color: #2980b9;
        }

        /* 添加输入验证样式 */
        input:invalid {
            border-color: #e74c3c;
        }

        /* 添加加载动画 */
        .loading {
            opacity: 0.7;
            pointer-events: none;
        }

        /* 提示消息样式 */
        .alert {
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 4px;
            display: none;
        }

        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
<div class="form-container">
    <h2 id="formTitle">个人信息设置</h2>

    <div id="alertSuccess" class="alert alert-success"></div>
    <div id="alertError" class="alert alert-error"></div>

    <form id="userForm">
        <input type="hidden" id="userId" name="userId" required>

        <div class="form-group">
            <label for="fullname">姓名:</label>
            <input type="text" id="fullname" name="fullname" required
                   placeholder="请输入姓名">
        </div>

        <div class="form-group">
            <label for="telephone">手机号:</label>
            <input type="text" id="telephone" name="telephone" required
                   pattern="[0-9]{11}" placeholder="请输入11位手机号码"
                   title="请输入11位手机号码">
        </div>

        <div class="form-group">
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" required
                   placeholder="请输入密码">
        </div>

        <button type="button" id="submitBtn" onclick="submitForm()">保存更改</button>
    </form>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const params = new URLSearchParams(window.location.search);
        const userId = params.get("userid");

        if (userId) {
            document.getElementById("formTitle").innerText = "个人信息设置";
            console.log(userId);
            console.log(`请求地址: /api/user/getUserInfo/`+userId);

            // 加载状态
            document.getElementById('submitBtn').classList.add('loading');

            axios.get(`/api/user/getUserInfo/`+userId)
                .then(response => {
                    if (response.data.code === 1) {
                        const user = response.data.data;
                        document.getElementById('userId').value = user.userId;
                        document.getElementById('fullname').value = user.fullname;
                        document.getElementById('telephone').value = user.telephone;
                        document.getElementById('password').value = user.password;
                    } else {
                        showError("未找到个人信息");
                    }
                })
                .catch(error => {
                    console.error("加载个人信息失败:", error);
                    showError("加载失败，请稍后重试");
                })
                .finally(() => {
                    document.getElementById('submitBtn').classList.remove('loading');
                });
        }
    });

    function showSuccess(message) {
        const alert = document.getElementById('alertSuccess');
        alert.textContent = message;
        alert.style.display = 'block';
        setTimeout(() => alert.style.display = 'none', 3000);
    }

    function showError(message) {
        const alert = document.getElementById('alertError');
        alert.textContent = message;
        alert.style.display = 'block';
        setTimeout(() => alert.style.display = 'none', 3000);
    }

    function submitForm() {
        const params = new URLSearchParams(window.location.search);
        const userId = params.get("userid");
        const fullname = $('#fullname').val().trim();
        const telephone = $('#telephone').val().trim();
        const password = $('#password').val().trim();

        if (!fullname || !telephone || !password) {
            showError("请完整填写表单内容");
            return;
        }

        const payload = {
            userid: userId,
            fullname: fullname,
            telephone: telephone,
            password: password
        };

        //提交按钮加载状态
        document.getElementById('submitBtn').classList.add('loading');

        axios.put('/api/user/userUpdate', payload)
            .then(response => {
                if (response.data.code === 1) {
                    showSuccess(response.data.message);
                    setTimeout(() => window.location.reload(), 1500);
                } else {
                    showError(response.data.message || "操作失败");
                }
            })
            .catch(error => {
                console.error("提交表单出错:", error);
                showError("提交失败，请稍后重试");
            })
            .finally(() => {
                document.getElementById('submitBtn').classList.remove('loading');
            });
    }
</script>
</body>
</html>
